<?php
class Sabai_Model_PageNavigator
{
    var $_pages;
    var $_currentPage;
    var $_linkUrl;
    var $_pageVar;
    var $_pageSummaryText;

    function Sabai_Model_PageNavigator(&$pages, $currentPage, $linkUrl = array(), $pageVar = 'page', $pageSummaryText = 'Page %1$d of %2$d')
    {
        $this->_pages =& $pages;
        $this->_currentPage = $currentPage;
        $this->_linkUrl = array_merge(array('base' => '',
                                            'params' => array(),
                                            'fragment' => ''),
                                      $linkUrl);
        $this->_pageVar = $pageVar;
        $this->_pageSummaryText = $pageSummaryText;
    }

    function process()
    {
        if ($this->_pages->size() <= 1) {
            return '';
        }
        $html = array();array(sprintf('<li class="pagesSummary">%s</li>', h(sprintf($this->_pageSummaryText, $this->_currentPage, $this->_pages->size()))));
        if ($this->_currentPage > 3) {
            $html[] = sprintf('<li class="pagesFirst">%s</li>', $this->_getFirstPageLink());
        }
        if ($this->_currentPage > 1) {
            $html[] = sprintf('<li class="pagesPrevious">%s</li>', $this->_getPreviousPageLink());
        }
        while ($p =& $this->_pages->getNext()) {
            if ($p->getPageNumber() == $this->_currentPage) {
                $html[] = sprintf('<li class="pagesCurrent">%d</li>', $p->getPageNumber());
            } elseif (abs($p->getPageNumber() - $this->_currentPage) <= 2) {
                $html[] = sprintf('<li>%s</li>', $this->_getPageLink($p->getPageNumber()));
            }
        }
        if ($this->_currentPage < $this->_pages->size()) {
            $html[] = sprintf('<li class="pagesNext">%s</li>', $this->_getNextPageLink());
        }
        if ($this->_currentPage < $this->_pages->size() - 2) {
            $html[] = sprintf('<li class="pagesLast">%s</li>', $this->_getLastPageLink());
        }
        return sprintf('<ul class="pages">%s</ul>', implode("", $html));
    }

    function _getFirstPageLink()
    {
        return sprintf('<a href="%s">&laquo;</a>', Sabai_URL::getByArray(array('base' => $this->_linkUrl['base'], 'params' => array_merge($this->_linkUrl['params'], array($this->_pageVar => 1)), 'fragment' => $this->_linkUrl['fragment'])));
    }

    function _getPreviousPageLink()
    {
        return sprintf('<a href="%s">&lsaquo;</a>', Sabai_URL::getByArray(array('base' => $this->_linkUrl['base'], 'params' => array_merge($this->_linkUrl['params'], array($this->_pageVar => $this->_currentPage - 1)), 'fragment' => $this->_linkUrl['fragment'])));
    }

    function _getPageLink($pageNum)
    {
        return sprintf('<a href="%s">%d</a>', Sabai_URL::getByArray(array('base' => $this->_linkUrl['base'], 'params' => array_merge($this->_linkUrl['params'], array($this->_pageVar => $pageNum)), 'fragment' => $this->_linkUrl['fragment'])), $pageNum);
    }

    function _getNextPageLink()
    {
        return sprintf('<a href="%s">&rsaquo</a>', Sabai_URL::getByArray(array('base' => $this->_linkUrl['base'], 'params' => array_merge($this->_linkUrl['params'], array($this->_pageVar => $this->_currentPage + 1)), 'fragment' => $this->_linkUrl['fragment'])));
    }

    function _getLastPageLink()
    {
        return sprintf('<a href="%s">&raquo;</a>', Sabai_URL::getByArray(array('base' => $this->_linkUrl['base'], 'params' => array_merge($this->_linkUrl['params'], array($this->_pageVar => $this->_pages->size())), 'fragment' => $this->_linkUrl['fragment'])));
    }
}